package com.ruoyi.web.schedule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.web.domain.MonitorArea;
import com.ruoyi.web.domain.dto.MonitorAreaDto;
import com.ruoyi.web.service.IMonitorAreaService;
import com.ruoyi.web.service.IMonitorCameraService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;

@Component
@Slf4j
public class CameraDeviceSchedule {

    @Autowired
    private IMonitorAreaService areaService;

    @Autowired
    private IMonitorCameraService cameraService;


    @Scheduled(cron = "0 0/30 * * * ?")
    public void syncToDayAccessCount(){
        List<MonitorArea> list = areaService.list(new LambdaQueryWrapper<MonitorArea>().eq(MonitorArea::getIsDelete, 0).eq(MonitorArea::getType, "2"));
        try{
            for (MonitorArea area : list) {
                MonitorAreaDto dto = new MonitorAreaDto();
                BeanUtils.copyProperties(area, dto);
                int cameraStatus = cameraService.getCameraStatus(dto);
                if(cameraStatus == 0){
                    areaService.update(new LambdaUpdateWrapper<MonitorArea>().set(MonitorArea::getCameraStatus, dto.getCameraStatus()).eq(MonitorArea::getBaseId, area.getBaseId()));
                }

            }
        }catch(Exception e){
            log.error("同步摄像头状态失败:{}",  ExceptionUtils.getMessage(e));
        }


    }

}
